# PAPER TITLE: The Dynamics of Racial Resentment Across the 50 U.S. States
# AUTHORS: Rebecca Kreitzer, Candis Smith, Feiya Suo
# EDITOR OF R DOCUMENTS: Feiya Suo
# FINAL EDIT DATE: July 2nd, 2019

# Install packages
# install.packages("readstata1")
library(readstata13)

# Set the working directory
setwd("/Users/feiyasuo/Documents/RA/racial/final")

# Read data
nber_1988_raw <- read.dta13("cpsmar88.dta")
nber_1990_raw <- read.dta13("cpsmar90.dta")
nber_1992_raw <- read.dta13("cpsmar92.dta")
nber_1994_raw <- read.dta13("cpsmar94.dta")
nber_2000_raw <- read.dta13("cpsmar00.dta")
nber_2004_raw <- read.dta13("cpsmar04.dta")
nber_2008_raw <- read.dta13("cpsmar08.dta")
nber_2012_raw <- read.dta13("cpsmar12.dta")
nber_2016_raw <- read.dta13("cpsmar2016.dta")

# Keep the variables we need
nber_2016 <- data.frame(household_id=nber_2016_raw$h_seq, year=2016, state=nber_2016_raw$gestfips, 
                        age=nber_2016_raw$a_age, gender=nber_2016_raw$a_sex, race_raw=nber_2016_raw$prdtrace, 
                        hispanic=nber_2016_raw$pehspnon, highest_education=nber_2016_raw$a_hga,stringsAsFactors=FALSE)

nber_2012 <- data.frame(household_id=nber_2012_raw$ph_seq, year=2012, state=nber_2012_raw$gestfips, 
                        age=nber_2012_raw$a_age, gender=nber_2012_raw$a_sex, race_raw=nber_2012_raw$prdtrace, 
                        hispanic=nber_2012_raw$pehspnon, highest_education=nber_2012_raw$a_hga,stringsAsFactors=FALSE)

nber_2008 <- data.frame(household_id=nber_2008_raw$ph_seq, year=2008, state=nber_2008_raw$gestfips, 
                        age=nber_2008_raw$a_age, gender=nber_2008_raw$a_sex, race_raw=nber_2008_raw$prdtrace, 
                        hispanic=nber_2008_raw$pehspnon, highest_education=nber_2008_raw$a_hga,stringsAsFactors=FALSE)

nber_2004 <- data.frame(household_id=nber_2004_raw$ph_seq, year=2004, state=nber_2004_raw$gestfips, 
                        age=nber_2004_raw$a_age, gender=nber_2004_raw$a_sex, race_raw=nber_2004_raw$prdtrace, 
                        hispanic=nber_2004_raw$pehspnon, highest_education=nber_2004_raw$a_hga,stringsAsFactors=FALSE)

nber_2000 <- data.frame(household_id=nber_2000_raw$ph_seq, year=2000, state=nber_2000_raw$hg_st60, 
                        age=nber_2000_raw$a_age, gender=nber_2000_raw$a_sex, race_raw=nber_2000_raw$a_race, 
                        hispanic=nber_2000_raw$fspanish, highest_education=nber_2000_raw$a_hga,stringsAsFactors=FALSE)

nber_1994 <- data.frame(household_id=nber_1994_raw$h_seq, year=1994, state=nber_1994_raw$grin_st, 
                        age=nber_1994_raw$a_age, gender=nber_1994_raw$a_sex, race_raw=nber_1994_raw$a_race, 
                        hispanic=nber_1994_raw$fspanish, highest_education=nber_1994_raw$a_hga,stringsAsFactors=FALSE)

nber_1992 <- data.frame(household_id=nber_1992_raw$h_seq, year=1992, state=nber_1992_raw$grin_st, 
                        age=nber_1992_raw$a_age, gender=nber_1992_raw$a_sex, race_raw=nber_1992_raw$a_race, 
                        hispanic=nber_1992_raw$fspanish, highest_education=nber_1992_raw$a_hga,stringsAsFactors=FALSE)

nber_1990 <- data.frame(household_id=nber_1990_raw$h_seq, year=1990, state=nber_1990_raw$grin_st, 
                        age=nber_1990_raw$a_age, gender=nber_1990_raw$a_sex, race_raw=nber_1990_raw$a_race, 
                        hispanic=nber_1990_raw$fspanish, highest_education=nber_1990_raw$a_hga,stringsAsFactors=FALSE)

nber_1988 <- data.frame(household_id=nber_1988_raw$h_seq, year=1988, state=nber_1988_raw$grin_st, 
                        age=nber_1988_raw$a_age, gender=nber_1988_raw$a_sex, race_raw=nber_1988_raw$a_race, 
                        hispanic=nber_1988_raw$fspanish, highest_education=nber_1988_raw$a_hga,stringsAsFactors=FALSE)

# Recode
### state: respondent's state(2-character), otherwise NA
nber_1988$state[nber_1988$state==01]<-"AL"
nber_1988$state[nber_1988$state==02]<-"AK"
nber_1988$state[nber_1988$state==04]<-"AZ"
nber_1988$state[nber_1988$state==05]<-"AR"
nber_1988$state[nber_1988$state==06]<-"CA"
nber_1988$state[nber_1988$state==08]<-"CO"
nber_1988$state[nber_1988$state==09]<-"CT"
nber_1988$state[nber_1988$state==10]<-"DE"
nber_1988$state[nber_1988$state==11]<-"DC"
nber_1988$state[nber_1988$state==12]<-"FL"
nber_1988$state[nber_1988$state==13]<-"GA"
nber_1988$state[nber_1988$state==15]<-"HI"
nber_1988$state[nber_1988$state==16]<-"ID"
nber_1988$state[nber_1988$state==17]<-"IL"
nber_1988$state[nber_1988$state==18]<-"IN"
nber_1988$state[nber_1988$state==19]<-"IA"
nber_1988$state[nber_1988$state==20]<-"KS"
nber_1988$state[nber_1988$state==21]<-"KY"
nber_1988$state[nber_1988$state==22]<-"LA"
nber_1988$state[nber_1988$state==23]<-"ME"
nber_1988$state[nber_1988$state==24]<-"MD"
nber_1988$state[nber_1988$state==25]<-"MA"
nber_1988$state[nber_1988$state==26]<-"MI"
nber_1988$state[nber_1988$state==27]<-"MN"
nber_1988$state[nber_1988$state==28]<-"MS"
nber_1988$state[nber_1988$state==29]<-"MO"
nber_1988$state[nber_1988$state==30]<-"MT"
nber_1988$state[nber_1988$state==31]<-"NE"
nber_1988$state[nber_1988$state==32]<-"NV"
nber_1988$state[nber_1988$state==33]<-"NH"
nber_1988$state[nber_1988$state==34]<-"NJ"
nber_1988$state[nber_1988$state==00]<-NA
nber_1988$state[nber_1988$state==35]<-"NM"
nber_1988$state[nber_1988$state==36]<-"NY"
nber_1988$state[nber_1988$state==37]<-"NC"
nber_1988$state[nber_1988$state==38]<-"ND"
nber_1988$state[nber_1988$state==39]<-"OH"
nber_1988$state[nber_1988$state==40]<-"OK"
nber_1988$state[nber_1988$state==41]<-"OR"
nber_1988$state[nber_1988$state==42]<-"PA"
nber_1988$state[nber_1988$state==44]<-"RI"
nber_1988$state[nber_1988$state==45]<-"SC"
nber_1988$state[nber_1988$state==46]<-"SD"
nber_1988$state[nber_1988$state==47]<-"TN"
nber_1988$state[nber_1988$state==48]<-"TX"
nber_1988$state[nber_1988$state==49]<-"UT"
nber_1988$state[nber_1988$state==50]<-"VT"
nber_1988$state[nber_1988$state==51]<-"VA"
nber_1988$state[nber_1988$state==53]<-"WA"
nber_1988$state[nber_1988$state==54]<-"WV"
nber_1988$state[nber_1988$state==55]<-"WI"
nber_1988$state[nber_1988$state==56]<-"WY"
nber_1988$state[nber_1988$state==96]<-NA

nber_1990$state <- as.character(nber_1990$state)
nber_1990$state[nber_1990$state=="Abroad"]<-NA
nber_1990$state[nber_1990$state=="Alabama"]<-"AL"
nber_1990$state[nber_1990$state=="Alaska"]<-"AK"
nber_1990$state[nber_1990$state=="Arizona"]<-"AZ"
nber_1990$state[nber_1990$state=="Arkansas"]<-"AR"
nber_1990$state[nber_1990$state=="California"]<-"CA"
nber_1990$state[nber_1990$state=="Colorado"]<-"CO"
nber_1990$state[nber_1990$state=="Connecticut"]<-"CT"
nber_1990$state[nber_1990$state=="Delaware"]<-"DE"
nber_1990$state[nber_1990$state=="District of Columbia"]<-"DC"
nber_1990$state[nber_1990$state=="Florida"]<-"FL"
nber_1990$state[nber_1990$state=="Georgia"]<-"GA"
nber_1990$state[nber_1990$state=="Hawaii"]<-"HI"
nber_1990$state[nber_1990$state=="Idaho"]<-"ID"
nber_1990$state[nber_1990$state=="Illinois"]<-"IL"
nber_1990$state[nber_1990$state=="Indiana"]<-"IN"
nber_1990$state[nber_1990$state=="Iowa"]<-"IA"
nber_1990$state[nber_1990$state=="Kansas"]<-"KS"
nber_1990$state[nber_1990$state=="Kentucky"]<-"KY"
nber_1990$state[nber_1990$state=="Louisiana"]<-"LA"
nber_1990$state[nber_1990$state=="Maine"]<-"ME"
nber_1990$state[nber_1990$state=="Maryland"]<-"MD"
nber_1990$state[nber_1990$state=="Massachusetts"]<-"MA"
nber_1990$state[nber_1990$state=="Michigan"]<-"MI"
nber_1990$state[nber_1990$state=="Minnesota"]<-"MN"
nber_1990$state[nber_1990$state=="Mississippi"]<-"MS"
nber_1990$state[nber_1990$state=="Missouri"]<-"MO"
nber_1990$state[nber_1990$state=="Montana"]<-"MT"
nber_1990$state[nber_1990$state=="Nebraska"]<-"NE"
nber_1990$state[nber_1990$state=="Nevada"]<-"NV"
nber_1990$state[nber_1990$state=="New Hampshire"]<-"NH"
nber_1990$state[nber_1990$state=="New Jersey"]<-"NJ"
nber_1990$state[nber_1990$state=="Not in universe, nonmover"]<-NA
nber_1990$state[nber_1990$state=="New Mexico"]<-"NM"
nber_1990$state[nber_1990$state=="New York"]<-"NY"
nber_1990$state[nber_1990$state=="North Carolina"]<-"NC"
nber_1990$state[nber_1990$state=="North Dakota"]<-"ND"
nber_1990$state[nber_1990$state=="Ohio"]<-"OH"
nber_1990$state[nber_1990$state=="Oklahoma"]<-"OK"
nber_1990$state[nber_1990$state=="Oregon"]<-"OR"
nber_1990$state[nber_1990$state=="Pennsylvania"]<-"PA"
nber_1990$state[nber_1990$state=="Rhode Island"]<-"RI"
nber_1990$state[nber_1990$state=="South Carolina"]<-"SC"
nber_1990$state[nber_1990$state=="South Dakota"]<-"SD"
nber_1990$state[nber_1990$state=="Tennessee"]<-"TN"
nber_1990$state[nber_1990$state=="Texas"]<-"TX"
nber_1990$state[nber_1990$state=="Utah"]<-"UT"
nber_1990$state[nber_1990$state=="Vermont"]<-"VT"
nber_1990$state[nber_1990$state=="Virginia"]<-"VA"
nber_1990$state[nber_1990$state=="Washington"]<-"WA"
nber_1990$state[nber_1990$state=="West Virginia"]<-"WV"
nber_1990$state[nber_1990$state=="Wisconsin"]<-"WI"
nber_1990$state[nber_1990$state=="Wyoming"]<-"WY"

nber_1992$state <- as.character(nber_1992$state)
nber_1992$state[nber_1992$state=="Abroad"]<-NA
nber_1992$state[nber_1992$state=="Alabama"]<-"AL"
nber_1992$state[nber_1992$state=="Alaska"]<-"AK"
nber_1992$state[nber_1992$state=="Arizona"]<-"AZ"
nber_1992$state[nber_1992$state=="Arkansas"]<-"AR"
nber_1992$state[nber_1992$state=="California"]<-"CA"
nber_1992$state[nber_1992$state=="Colorado"]<-"CO"
nber_1992$state[nber_1992$state=="Connecticut"]<-"CT"
nber_1992$state[nber_1992$state=="Delaware"]<-"DE"
nber_1992$state[nber_1992$state=="District of Columbia"]<-"DC"
nber_1992$state[nber_1992$state=="Florida"]<-"FL"
nber_1992$state[nber_1992$state=="Georgia"]<-"GA"
nber_1992$state[nber_1992$state=="Hawaii"]<-"HI"
nber_1992$state[nber_1992$state=="Idaho"]<-"ID"
nber_1992$state[nber_1992$state=="Illinois"]<-"IL"
nber_1992$state[nber_1992$state=="Indiana"]<-"IN"
nber_1992$state[nber_1992$state=="Iowa"]<-"IA"
nber_1992$state[nber_1992$state=="Kansas"]<-"KS"
nber_1992$state[nber_1992$state=="Kentucky"]<-"KY"
nber_1992$state[nber_1992$state=="Louisiana"]<-"LA"
nber_1992$state[nber_1992$state=="Maine"]<-"ME"
nber_1992$state[nber_1992$state=="Maryland"]<-"MD"
nber_1992$state[nber_1992$state=="Massachusetts"]<-"MA"
nber_1992$state[nber_1992$state=="Michigan"]<-"MI"
nber_1992$state[nber_1992$state=="Minnesota"]<-"MN"
nber_1992$state[nber_1992$state=="Mississippi"]<-"MS"
nber_1992$state[nber_1992$state=="Missouri"]<-"MO"
nber_1992$state[nber_1992$state=="Montana"]<-"MT"
nber_1992$state[nber_1992$state=="Nebraska"]<-"NE"
nber_1992$state[nber_1992$state=="Nevada"]<-"NV"
nber_1992$state[nber_1992$state=="New Hampshire"]<-"NH"
nber_1992$state[nber_1992$state=="New Jersey"]<-"NJ"
nber_1992$state[nber_1992$state=="Not in universe, nonmover"]<-NA
nber_1992$state[nber_1992$state=="New Mexico"]<-"NM"
nber_1992$state[nber_1992$state=="New York"]<-"NY"
nber_1992$state[nber_1992$state=="North Carolina"]<-"NC"
nber_1992$state[nber_1992$state=="North Dakota"]<-"ND"
nber_1992$state[nber_1992$state=="Ohio"]<-"OH"
nber_1992$state[nber_1992$state=="Oklahoma"]<-"OK"
nber_1992$state[nber_1992$state=="Oregon"]<-"OR"
nber_1992$state[nber_1992$state=="Pennsylvania"]<-"PA"
nber_1992$state[nber_1992$state=="Rhode Island"]<-"RI"
nber_1992$state[nber_1992$state=="South Carolina"]<-"SC"
nber_1992$state[nber_1992$state=="South Dakota"]<-"SD"
nber_1992$state[nber_1992$state=="Tennessee"]<-"TN"
nber_1992$state[nber_1992$state=="Texas"]<-"TX"
nber_1992$state[nber_1992$state=="Utah"]<-"UT"
nber_1992$state[nber_1992$state=="Vermont"]<-"VT"
nber_1992$state[nber_1992$state=="Virginia"]<-"VA"
nber_1992$state[nber_1992$state=="Washington"]<-"WA"
nber_1992$state[nber_1992$state=="West Virginia"]<-"WV"
nber_1992$state[nber_1992$state=="Wisconsin"]<-"WI"
nber_1992$state[nber_1992$state=="Wyoming"]<-"WY"

nber_1994$state <- as.character(nber_1994$state)
nber_1994$state[nber_1994$state=="Abroad"]<-NA
nber_1994$state[nber_1994$state=="Alabama"]<-"AL"
nber_1994$state[nber_1994$state=="Alaska"]<-"AK"
nber_1994$state[nber_1994$state=="Arizona"]<-"AZ"
nber_1994$state[nber_1994$state=="Arkansas"]<-"AR"
nber_1994$state[nber_1994$state=="California"]<-"CA"
nber_1994$state[nber_1994$state=="Colorado"]<-"CO"
nber_1994$state[nber_1994$state=="Connecticut"]<-"CT"
nber_1994$state[nber_1994$state=="Delaware"]<-"DE"
nber_1994$state[nber_1994$state=="District of Columbia"]<-"DC"
nber_1994$state[nber_1994$state=="Florida"]<-"FL"
nber_1994$state[nber_1994$state=="Georgia"]<-"GA"
nber_1994$state[nber_1994$state=="Hawaii"]<-"HI"
nber_1994$state[nber_1994$state=="Idaho"]<-"ID"
nber_1994$state[nber_1994$state=="Illinois"]<-"IL"
nber_1994$state[nber_1994$state=="Indiana"]<-"IN"
nber_1994$state[nber_1994$state=="Iowa"]<-"IA"
nber_1994$state[nber_1994$state=="Kansas"]<-"KS"
nber_1994$state[nber_1994$state=="Kentucky"]<-"KY"
nber_1994$state[nber_1994$state=="Louisiana"]<-"LA"
nber_1994$state[nber_1994$state=="Maine"]<-"ME"
nber_1994$state[nber_1994$state=="Maryland"]<-"MD"
nber_1994$state[nber_1994$state=="Massachusetts"]<-"MA"
nber_1994$state[nber_1994$state=="Michigan"]<-"MI"
nber_1994$state[nber_1994$state=="Minnesota"]<-"MN"
nber_1994$state[nber_1994$state=="Mississippi"]<-"MS"
nber_1994$state[nber_1994$state=="Missouri"]<-"MO"
nber_1994$state[nber_1994$state=="Montana"]<-"MT"
nber_1994$state[nber_1994$state=="Nebraska"]<-"NE"
nber_1994$state[nber_1994$state=="Nevada"]<-"NV"
nber_1994$state[nber_1994$state=="New Hampshire"]<-"NH"
nber_1994$state[nber_1994$state=="New Jersey"]<-"NJ"
nber_1994$state[nber_1994$state=="Not in universe, nonmover"]<-NA
nber_1994$state[nber_1994$state=="New Mexico"]<-"NM"
nber_1994$state[nber_1994$state=="New York"]<-"NY"
nber_1994$state[nber_1994$state=="North Carolina"]<-"NC"
nber_1994$state[nber_1994$state=="North Dakota"]<-"ND"
nber_1994$state[nber_1994$state=="Ohio"]<-"OH"
nber_1994$state[nber_1994$state=="Oklahoma"]<-"OK"
nber_1994$state[nber_1994$state=="Oregon"]<-"OR"
nber_1994$state[nber_1994$state=="Pennsylvania"]<-"PA"
nber_1994$state[nber_1994$state=="Rhode Island"]<-"RI"
nber_1994$state[nber_1994$state=="South Carolina"]<-"SC"
nber_1994$state[nber_1994$state=="South Dakota"]<-"SD"
nber_1994$state[nber_1994$state=="Tennessee"]<-"TN"
nber_1994$state[nber_1994$state=="Texas"]<-"TX"
nber_1994$state[nber_1994$state=="Utah"]<-"UT"
nber_1994$state[nber_1994$state=="Vermont"]<-"VT"
nber_1994$state[nber_1994$state=="Virginia"]<-"VA"
nber_1994$state[nber_1994$state=="Washington"]<-"WA"
nber_1994$state[nber_1994$state=="West Virginia"]<-"WV"
nber_1994$state[nber_1994$state=="Wisconsin"]<-"WI"
nber_1994$state[nber_1994$state=="Wyoming"]<-"WY"

nber_2000$state <- as.character(nber_2000$state)
nber_2000$state[nber_2000$state=="Abroad"]<-NA
nber_2000$state[nber_2000$state=="Alabama"]<-"AL"
nber_2000$state[nber_2000$state=="Alaska"]<-"AK"
nber_2000$state[nber_2000$state=="Arizona"]<-"AZ"
nber_2000$state[nber_2000$state=="Arkansas"]<-"AR"
nber_2000$state[nber_2000$state=="California"]<-"CA"
nber_2000$state[nber_2000$state=="Colorado"]<-"CO"
nber_2000$state[nber_2000$state=="Connecticut"]<-"CT"
nber_2000$state[nber_2000$state=="Delaware"]<-"DE"
nber_2000$state[nber_2000$state=="District of Columbia"]<-"DC"
nber_2000$state[nber_2000$state=="Florida"]<-"FL"
nber_2000$state[nber_2000$state=="Georgia"]<-"GA"
nber_2000$state[nber_2000$state=="Hawaii"]<-"HI"
nber_2000$state[nber_2000$state=="Idaho"]<-"ID"
nber_2000$state[nber_2000$state=="Illinois"]<-"IL"
nber_2000$state[nber_2000$state=="Indiana"]<-"IN"
nber_2000$state[nber_2000$state=="Iowa"]<-"IA"
nber_2000$state[nber_2000$state=="Kansas"]<-"KS"
nber_2000$state[nber_2000$state=="Kentucky"]<-"KY"
nber_2000$state[nber_2000$state=="Louisiana"]<-"LA"
nber_2000$state[nber_2000$state=="Maine"]<-"ME"
nber_2000$state[nber_2000$state=="Maryland"]<-"MD"
nber_2000$state[nber_2000$state=="Massachusetts"]<-"MA"
nber_2000$state[nber_2000$state=="Michigan"]<-"MI"
nber_2000$state[nber_2000$state=="Minnesota"]<-"MN"
nber_2000$state[nber_2000$state=="Mississippi"]<-"MS"
nber_2000$state[nber_2000$state=="Missouri"]<-"MO"
nber_2000$state[nber_2000$state=="Montana"]<-"MT"
nber_2000$state[nber_2000$state=="Nebraska"]<-"NE"
nber_2000$state[nber_2000$state=="Nevada"]<-"NV"
nber_2000$state[nber_2000$state=="New Hampshire"]<-"NH"
nber_2000$state[nber_2000$state=="New Jersey"]<-"NJ"
nber_2000$state[nber_2000$state=="Not in universe, nonmover"]<-NA
nber_2000$state[nber_2000$state=="New Mexico"]<-"NM"
nber_2000$state[nber_2000$state=="New York"]<-"NY"
nber_2000$state[nber_2000$state=="North Carolina"]<-"NC"
nber_2000$state[nber_2000$state=="North Dakota"]<-"ND"
nber_2000$state[nber_2000$state=="Ohio"]<-"OH"
nber_2000$state[nber_2000$state=="Oklahoma"]<-"OK"
nber_2000$state[nber_2000$state=="Oregon"]<-"OR"
nber_2000$state[nber_2000$state=="Pennsylvania"]<-"PA"
nber_2000$state[nber_2000$state=="Rhode Island"]<-"RI"
nber_2000$state[nber_2000$state=="South Carolina"]<-"SC"
nber_2000$state[nber_2000$state=="South Dakota"]<-"SD"
nber_2000$state[nber_2000$state=="Tennessee"]<-"TN"
nber_2000$state[nber_2000$state=="Texas"]<-"TX"
nber_2000$state[nber_2000$state=="Utah"]<-"UT"
nber_2000$state[nber_2000$state=="Vermont"]<-"VT"
nber_2000$state[nber_2000$state=="Virginia"]<-"VA"
nber_2000$state[nber_2000$state=="Washington"]<-"WA"
nber_2000$state[nber_2000$state=="West Virginia"]<-"WV"
nber_2000$state[nber_2000$state=="Wisconsin"]<-"WI"
nber_2000$state[nber_2000$state=="Wyoming"]<-"WY"

nber_2004$state[nber_2004$state==01]<-"AL"
nber_2004$state[nber_2004$state==02]<-"AK"
nber_2004$state[nber_2004$state==04]<-"AZ"
nber_2004$state[nber_2004$state==05]<-"AR"
nber_2004$state[nber_2004$state==06]<-"CA"
nber_2004$state[nber_2004$state==08]<-"CO"
nber_2004$state[nber_2004$state==09]<-"CT"
nber_2004$state[nber_2004$state==10]<-"DE"
nber_2004$state[nber_2004$state==11]<-"DC"
nber_2004$state[nber_2004$state==12]<-"FL"
nber_2004$state[nber_2004$state==13]<-"GA"
nber_2004$state[nber_2004$state==15]<-"HI"
nber_2004$state[nber_2004$state==16]<-"ID"
nber_2004$state[nber_2004$state==17]<-"IL"
nber_2004$state[nber_2004$state==18]<-"IN"
nber_2004$state[nber_2004$state==19]<-"IA"
nber_2004$state[nber_2004$state==20]<-"KS"
nber_2004$state[nber_2004$state==21]<-"KY"
nber_2004$state[nber_2004$state==22]<-"LA"
nber_2004$state[nber_2004$state==23]<-"ME"
nber_2004$state[nber_2004$state==24]<-"MD"
nber_2004$state[nber_2004$state==25]<-"MA"
nber_2004$state[nber_2004$state==26]<-"MI"
nber_2004$state[nber_2004$state==27]<-"MN"
nber_2004$state[nber_2004$state==28]<-"MS"
nber_2004$state[nber_2004$state==29]<-"MO"
nber_2004$state[nber_2004$state==30]<-"MT"
nber_2004$state[nber_2004$state==31]<-"NE"
nber_2004$state[nber_2004$state==32]<-"NV"
nber_2004$state[nber_2004$state==33]<-"NH"
nber_2004$state[nber_2004$state==34]<-"NJ"
nber_2004$state[nber_2004$state==00]<-NA
nber_2004$state[nber_2004$state==35]<-"NM"
nber_2004$state[nber_2004$state==36]<-"NY"
nber_2004$state[nber_2004$state==37]<-"NC"
nber_2004$state[nber_2004$state==38]<-"ND"
nber_2004$state[nber_2004$state==39]<-"OH"
nber_2004$state[nber_2004$state==40]<-"OK"
nber_2004$state[nber_2004$state==41]<-"OR"
nber_2004$state[nber_2004$state==42]<-"PA"
nber_2004$state[nber_2004$state==44]<-"RI"
nber_2004$state[nber_2004$state==45]<-"SC"
nber_2004$state[nber_2004$state==46]<-"SD"
nber_2004$state[nber_2004$state==47]<-"TN"
nber_2004$state[nber_2004$state==48]<-"TX"
nber_2004$state[nber_2004$state==49]<-"UT"
nber_2004$state[nber_2004$state==50]<-"VT"
nber_2004$state[nber_2004$state==51]<-"VA"
nber_2004$state[nber_2004$state==53]<-"WA"
nber_2004$state[nber_2004$state==54]<-"WV"
nber_2004$state[nber_2004$state==55]<-"WI"
nber_2004$state[nber_2004$state==56]<-"WY"

nber_2008$state[nber_2008$state==01]<-"AL"
nber_2008$state[nber_2008$state==02]<-"AK"
nber_2008$state[nber_2008$state==04]<-"AZ"
nber_2008$state[nber_2008$state==05]<-"AR"
nber_2008$state[nber_2008$state==06]<-"CA"
nber_2008$state[nber_2008$state==08]<-"CO"
nber_2008$state[nber_2008$state==09]<-"CT"
nber_2008$state[nber_2008$state==10]<-"DE"
nber_2008$state[nber_2008$state==11]<-"DC"
nber_2008$state[nber_2008$state==12]<-"FL"
nber_2008$state[nber_2008$state==13]<-"GA"
nber_2008$state[nber_2008$state==15]<-"HI"
nber_2008$state[nber_2008$state==16]<-"ID"
nber_2008$state[nber_2008$state==17]<-"IL"
nber_2008$state[nber_2008$state==18]<-"IN"
nber_2008$state[nber_2008$state==19]<-"IA"
nber_2008$state[nber_2008$state==20]<-"KS"
nber_2008$state[nber_2008$state==21]<-"KY"
nber_2008$state[nber_2008$state==22]<-"LA"
nber_2008$state[nber_2008$state==23]<-"ME"
nber_2008$state[nber_2008$state==24]<-"MD"
nber_2008$state[nber_2008$state==25]<-"MA"
nber_2008$state[nber_2008$state==26]<-"MI"
nber_2008$state[nber_2008$state==27]<-"MN"
nber_2008$state[nber_2008$state==28]<-"MS"
nber_2008$state[nber_2008$state==29]<-"MO"
nber_2008$state[nber_2008$state==30]<-"MT"
nber_2008$state[nber_2008$state==31]<-"NE"
nber_2008$state[nber_2008$state==32]<-"NV"
nber_2008$state[nber_2008$state==33]<-"NH"
nber_2008$state[nber_2008$state==34]<-"NJ"
nber_2008$state[nber_2008$state==00]<-NA
nber_2008$state[nber_2008$state==35]<-"NM"
nber_2008$state[nber_2008$state==36]<-"NY"
nber_2008$state[nber_2008$state==37]<-"NC"
nber_2008$state[nber_2008$state==38]<-"ND"
nber_2008$state[nber_2008$state==39]<-"OH"
nber_2008$state[nber_2008$state==40]<-"OK"
nber_2008$state[nber_2008$state==41]<-"OR"
nber_2008$state[nber_2008$state==42]<-"PA"
nber_2008$state[nber_2008$state==44]<-"RI"
nber_2008$state[nber_2008$state==45]<-"SC"
nber_2008$state[nber_2008$state==46]<-"SD"
nber_2008$state[nber_2008$state==47]<-"TN"
nber_2008$state[nber_2008$state==48]<-"TX"
nber_2008$state[nber_2008$state==49]<-"UT"
nber_2008$state[nber_2008$state==50]<-"VT"
nber_2008$state[nber_2008$state==51]<-"VA"
nber_2008$state[nber_2008$state==53]<-"WA"
nber_2008$state[nber_2008$state==54]<-"WV"
nber_2008$state[nber_2008$state==55]<-"WI"
nber_2008$state[nber_2008$state==56]<-"WY"

nber_2012$state[nber_2012$state==01]<-"AL"
nber_2012$state[nber_2012$state==02]<-"AK"
nber_2012$state[nber_2012$state==04]<-"AZ"
nber_2012$state[nber_2012$state==05]<-"AR"
nber_2012$state[nber_2012$state==06]<-"CA"
nber_2012$state[nber_2012$state==08]<-"CO"
nber_2012$state[nber_2012$state==09]<-"CT"
nber_2012$state[nber_2012$state==10]<-"DE"
nber_2012$state[nber_2012$state==11]<-"DC"
nber_2012$state[nber_2012$state==12]<-"FL"
nber_2012$state[nber_2012$state==13]<-"GA"
nber_2012$state[nber_2012$state==15]<-"HI"
nber_2012$state[nber_2012$state==16]<-"ID"
nber_2012$state[nber_2012$state==17]<-"IL"
nber_2012$state[nber_2012$state==18]<-"IN"
nber_2012$state[nber_2012$state==19]<-"IA"
nber_2012$state[nber_2012$state==20]<-"KS"
nber_2012$state[nber_2012$state==21]<-"KY"
nber_2012$state[nber_2012$state==22]<-"LA"
nber_2012$state[nber_2012$state==23]<-"ME"
nber_2012$state[nber_2012$state==24]<-"MD"
nber_2012$state[nber_2012$state==25]<-"MA"
nber_2012$state[nber_2012$state==26]<-"MI"
nber_2012$state[nber_2012$state==27]<-"MN"
nber_2012$state[nber_2012$state==28]<-"MS"
nber_2012$state[nber_2012$state==29]<-"MO"
nber_2012$state[nber_2012$state==30]<-"MT"
nber_2012$state[nber_2012$state==31]<-"NE"
nber_2012$state[nber_2012$state==32]<-"NV"
nber_2012$state[nber_2012$state==33]<-"NH"
nber_2012$state[nber_2012$state==34]<-"NJ"
nber_2012$state[nber_2012$state==00]<-NA
nber_2012$state[nber_2012$state==35]<-"NM"
nber_2012$state[nber_2012$state==36]<-"NY"
nber_2012$state[nber_2012$state==37]<-"NC"
nber_2012$state[nber_2012$state==38]<-"ND"
nber_2012$state[nber_2012$state==39]<-"OH"
nber_2012$state[nber_2012$state==40]<-"OK"
nber_2012$state[nber_2012$state==41]<-"OR"
nber_2012$state[nber_2012$state==42]<-"PA"
nber_2012$state[nber_2012$state==44]<-"RI"
nber_2012$state[nber_2012$state==45]<-"SC"
nber_2012$state[nber_2012$state==46]<-"SD"
nber_2012$state[nber_2012$state==47]<-"TN"
nber_2012$state[nber_2012$state==48]<-"TX"
nber_2012$state[nber_2012$state==49]<-"UT"
nber_2012$state[nber_2012$state==50]<-"VT"
nber_2012$state[nber_2012$state==51]<-"VA"
nber_2012$state[nber_2012$state==53]<-"WA"
nber_2012$state[nber_2012$state==54]<-"WV"
nber_2012$state[nber_2012$state==55]<-"WI"
nber_2012$state[nber_2012$state==56]<-"WY"

### highest_education: 1=Grade school or less (0-8 grades), 2=High school (12 grades or fewer, incl. non-college, 3=Some college (13 grades or more but no degree/associate degree), 4=College or advanced degree
nber_2016$highest_education <- as.character(nber_2016$highest_education)
nber_2016$highest_education[nber_2016$highest_education=="5th or 6th grade" | nber_2016$highest_education==
                              "7th and 8th grade" | nber_2016$highest_education=="1st,2nd,3rd,or 4th grade" |
                              nber_2016$highest_education=="Less than 1st grade"] <- 1
nber_2016$highest_education[nber_2016$highest_education=="10th grade" | nber_2016$highest_education=="11th grade" |
                              nber_2016$highest_education=="12th grade no diploma" | nber_2016$highest_education==
                              "9th grade"| nber_2016$highest_education==
                              "High school graduate - high school diploma or equivalent"] <- 2
nber_2016$highest_education[nber_2016$highest_education=="Associate degree in college - academic program" |
                              nber_2016$highest_education=="Associate degree in college - occupation/vocation program" |
                              nber_2016$highest_education=="Some college but no degree"] <-3
nber_2016$highest_education[nber_2016$highest_education=="Bachelor's degree (for example: BA,AB,BS)" | 
                              nber_2016$highest_education=="Doctorate degree (for example: PHD,EDD)" | 
                              nber_2016$highest_education=="Master's degree (for example: MA,MS,MENG,MED,MSW, MBA)" | 
                              nber_2016$highest_education=="Professional school degree (for example: MD,DDS,DVM,LLB,JD)"] <-4

nber_2012$highest_education <- as.character(nber_2012$highest_education)
nber_2012$highest_education[nber_2012$highest_education=="5th or 6th grade" | nber_2012$highest_education==
                              "7th and 8th grade" | nber_2012$highest_education=="1st,2nd,3rd,or 4th grade" |
                              nber_2012$highest_education=="Less than 1st grade"] <- 1
nber_2012$highest_education[nber_2012$highest_education=="10th grade" | nber_2012$highest_education=="11th grade" |
                              nber_2012$highest_education=="12th grade no diploma" | nber_2012$highest_education==
                              "9th grade"| nber_2012$highest_education==
                              "High school graduate - high"] <- 2
nber_2012$highest_education[nber_2012$highest_education=="Associate degree in college -_(41)" |
                              nber_2012$highest_education=="Associate degree in college -_(42)" |
                              nber_2012$highest_education=="Some college but no degree"] <-3
nber_2012$highest_education[nber_2012$highest_education=="Bachelor's degree (for" | 
                              nber_2012$highest_education=="Doctorate degree (for" | 
                              nber_2012$highest_education=="Master's degree (for" | 
                              nber_2012$highest_education=="Professional school degree (for"] <-4

nber_2008$highest_education <- as.character(nber_2008$highest_education)
nber_2008$highest_education[nber_2008$highest_education=="5th or 6th grade" | nber_2008$highest_education==
                              "7th and 8th grade" | nber_2008$highest_education=="1st,2nd,3rd,or 4th grade" |
                              nber_2008$highest_education=="Less than 1st grade"] <- 1
nber_2008$highest_education[nber_2008$highest_education=="10th grade" | nber_2008$highest_education=="11th grade" |
                              nber_2008$highest_education=="12th grade no diploma" | nber_2008$highest_education==
                              "9th grade"| nber_2008$highest_education==
                              "High school graduate - high"] <- 2
nber_2008$highest_education[nber_2008$highest_education=="Associate degree in college -_(41)" |
                              nber_2008$highest_education=="Associate degree in college -_(42)" |
                              nber_2008$highest_education=="Some college but no degree"] <-3
nber_2008$highest_education[nber_2008$highest_education=="Bachelor's degree (for" | 
                              nber_2008$highest_education=="Doctorate degree (for" | 
                              nber_2008$highest_education=="Master's degree (for" | 
                              nber_2008$highest_education=="Professional school degree (for"] <-4

nber_2004$highest_education <- as.character(nber_2004$highest_education)
nber_2004$highest_education[nber_2004$highest_education=="5th or 6th grade" | nber_2004$highest_education==
                              "7th and 8th grade" | nber_2004$highest_education=="1st,2nd,3rd,or 4th grade" |
                              nber_2004$highest_education=="Less than 1st grade"] <- 1
nber_2004$highest_education[nber_2004$highest_education=="10th grade" | nber_2004$highest_education=="11th grade" |
                              nber_2004$highest_education=="12th grade no diploma" | nber_2004$highest_education==
                              "9th grade"| nber_2004$highest_education==
                              "High school graduate - high"] <- 2
nber_2004$highest_education[nber_2004$highest_education=="Associate degree in college -_(41)" |
                              nber_2004$highest_education=="Associate degree in college -_(42)" |
                              nber_2004$highest_education=="Some college but no degree"] <-3
nber_2004$highest_education[nber_2004$highest_education=="Bachelor's degree (for" | 
                              nber_2004$highest_education=="Doctorate degree (for" | 
                              nber_2004$highest_education=="Master's degree (for" | 
                              nber_2004$highest_education=="Professional school degree (for"] <-4

nber_2000$highest_education <- as.character(nber_2000$highest_education)
nber_2000$highest_education[nber_2000$highest_education=="5th or 6th grade" | nber_2000$highest_education==
                              "7th and 8th grade" | nber_2000$highest_education=="1st,2nd,3rd,or 4th grade" |
                              nber_2000$highest_education=="Less than 1st grade"] <- 1
nber_2000$highest_education[nber_2000$highest_education=="10th grade" | nber_2000$highest_education=="11th grade" |
                              nber_2000$highest_education=="12th grade no diploma" | nber_2000$highest_education==
                              "9th grade"| nber_2000$highest_education==
                              "High school graduate - high"] <- 2
nber_2000$highest_education[nber_2000$highest_education=="Associate degree in college -_(41)" |
                              nber_2000$highest_education=="Associate degree in college -_(42)" |
                              nber_2000$highest_education=="Some college but no degree"] <-3
nber_2000$highest_education[nber_2000$highest_education=="Bachelor's degree (for" | 
                              nber_2000$highest_education=="Doctorate degree (for" | 
                              nber_2000$highest_education=="Master's degree (for" | 
                              nber_2000$highest_education=="Professional school degree (for"] <-4

nber_1994$highest_education <- as.character(nber_1994$highest_education)
nber_1994$highest_education[nber_1994$highest_education=="5th or 6th grade" | nber_1994$highest_education==
                              "7th and 8th grade" | nber_1994$highest_education=="1st,2nd,3rd,or 4th grade" |
                              nber_1994$highest_education=="Less than 1st grade"] <- 1
nber_1994$highest_education[nber_1994$highest_education=="10th grade" | nber_1994$highest_education=="11th grade" |
                              nber_1994$highest_education=="12th grade no diploma" | nber_1994$highest_education==
                              "9th grade"| nber_1994$highest_education==
                              "High school graduate - high"] <- 2
nber_1994$highest_education[nber_1994$highest_education=="Associate degree in college -_(41)" |
                              nber_1994$highest_education=="Associate degree in college -_(42)" |
                              nber_1994$highest_education=="Some college but no degree"] <-3
nber_1994$highest_education[nber_1994$highest_education=="Bachelor's degree (for example:" | 
                              nber_1994$highest_education=="Doctorate degree (for example:" | 
                              nber_1994$highest_education=="Master's degree (for example:" | 
                              nber_1994$highest_education=="Professional school degree"] <-4

nber_1992$highest_education <- as.character(nber_1992$highest_education)
nber_1992$highest_education[nber_1992$highest_education=="5th or 6th grade" | nber_1992$highest_education==
                              "7th and 8th grade" | nber_1992$highest_education=="1st, 2nd, 3rd, or 4th grade" |
                              nber_1992$highest_education=="Less than 1st grade"] <- 1
nber_1992$highest_education[nber_1992$highest_education=="10th grade" | nber_1992$highest_education=="11th grade" |
                              nber_1992$highest_education=="12th grade no diploma" | nber_1992$highest_education==
                              "9th grade"| nber_1992$highest_education==
                              "High school graduate-High school"] <- 2
nber_1992$highest_education[nber_1992$highest_education=="Associate's degree in college -_(41)" |
                              nber_1992$highest_education=="Associate's degree in college -_(42)" |
                              nber_1992$highest_education=="Some college but no degree"] <-3
nber_1992$highest_education[nber_1992$highest_education=="Bachelor's degree (for example:  BA," | 
                              nber_1992$highest_education=="Doctorate degree (for example:" | 
                              nber_1992$highest_education=="Master's degree (for example:  MA, MS," | 
                              nber_1992$highest_education=="Professional school degree (for"] <-4

nber_1990$highest_education <- as.character(nber_1990$highest_education)
nber_1990$highest_education[nber_1990$highest_education=="E1" | nber_1990$highest_education=="E2" |
                              nber_1990$highest_education=="E3" | nber_1990$highest_education=="E4" |
                              nber_1990$highest_education=="E5" | nber_1990$highest_education=="E6" |
                              nber_1990$highest_education=="E7" | nber_1990$highest_education=="E8" |
                              nber_1990$highest_education=="None , kindergarten or children"] <- 1
nber_1990$highest_education[nber_1990$highest_education=="H1" | nber_1990$highest_education=="H2" |
                              nber_1990$highest_education=="H3" | nber_1990$highest_education=="H4"] <- 2
nber_1990$highest_education[nber_1990$highest_education=="C1" | nber_1990$highest_education=="C2" |
                              nber_1990$highest_education=="C3"] <- 3
nber_1990$highest_education[nber_1990$highest_education=="C4" | nber_1990$highest_education=="C5" |
                              nber_1990$highest_education=="C6+"] <- 4

nber_1988$highest_education <- as.character(nber_1988$highest_education)
nber_1988$highest_education[nber_1988$highest_education=="E1" | nber_1988$highest_education=="E2" |
                              nber_1988$highest_education=="E3" | nber_1988$highest_education=="E4" |
                              nber_1988$highest_education=="E5" | nber_1988$highest_education=="E6" |
                              nber_1988$highest_education=="E7" | nber_1988$highest_education=="E8" |
                              nber_1988$highest_education=="None , kindergarten or children"] <- 1
nber_1988$highest_education[nber_1988$highest_education=="H1" | nber_1988$highest_education=="H2" |
                              nber_1988$highest_education=="H3" | nber_1988$highest_education=="H4"] <- 2
nber_1988$highest_education[nber_1988$highest_education=="C1" | nber_1988$highest_education=="C2" |
                              nber_1988$highest_education=="C3"] <- 3
nber_1988$highest_education[nber_1988$highest_education=="C4" | nber_1988$highest_education=="C5" |
                              nber_1988$highest_education=="C6+"] <- 4

# Combine the data
nber <- rbind(nber_1988, nber_1990, nber_1992, nber_1994, nber_2000, nber_2004, nber_2008, nber_2012, nber_2016)

### age: only keep adults
nber <- nber[nber$age>=18,]

nber <- nber[is.na(nber$state)==F,]

### set up age group: 1=18-29, 2=30-44, 3=45-64, 4=65+, otherwise NA
nber$age_group[nber$age>=18 & nber$age<=29] <- 1
nber$age_group[nber$age>=30 & nber$age<=44] <- 2
nber$age_group[nber$age>=45 & nber$age<=64] <- 3
nber$age_group[nber$age>=65] <- 4

### gender: if women=1, if man=0
nber$gender <- ifelse(nber$gender=="Female",1,0)

### race: 1=White non-Hispanic, 2=Black non-Hispanic, 3=Other
nber$race_raw <- as.character(nber$race_raw)
nber$hispanic <- as.character(nber$hispanic)

nber$race[nber$race_raw=="White only" & nber$hispanic=="No"] <- 1
nber$race[nber$race_raw=="Black only" & nber$hispanic=="No"] <- 2
nber$race[nber$race_raw!="White only" & nber$race_raw!="Black only"] <- 3
nber$race[nber$hispanic=="Yes"] <- 3

# six binary variables
nber$white_male <- ifelse(nber$race==1 & nber$gender==0,1,0)
nber$white_female <- ifelse(nber$race==1 & nber$gender==1,1,0)
nber$black_male <- ifelse(nber$race==2 & nber$gender==0,1,0)
nber$black_female <- ifelse(nber$race==2 & nber$gender==1,1,0)
nber$other_male <- ifelse(nber$race==3 & nber$gender==0,1,0)
nber$other_female <- ifelse(nber$race==3 & nber$gender==1,1,0)

# Correct Level & ideology matching
nber$state[nber$state=="NA"] <- NA
nber$state<-as.factor(nber$state)
nber$state <- droplevels(nber$state, "District Of Columbia")

nber$state <- as.character(nber$state)
nber <- nber[nber$state!="DC",] #do not have DC data in ideology measurement

ideology <- read.csv("ideology.csv")
ideology <- ideology[ideology$year>=1988,]
ideology <- ideology[ideology$state!="DC",]

nber$ideology = NA
for (i in 1:nrow(nber)) {
  if (length(ideology$measure[ideology$state==nber$state[i] & ideology$year==nber$year[i]])==1)
    nber$ideology[i] <- 
      ideology$measure[ideology$state==nber$state[i] & ideology$year==nber$year[i]]
}


# Write the data
write.csv(nber, "nber_final.csv")
